home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / htmlsample / history.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  8.3 KB  |  269 lines

  1. /*
  2.     file History.c
  3.     
  4.     Description:
  5.     These routines are used to store visitled links.
  6.     
  7.     HTMLSample is an application illustrating how to use the new
  8.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  9.     is Apple's light-weight HTML rendering engine capable of
  10.     displaying HTML files.
  11.  
  12.     by John Montbriand, 1999.
  13.  
  14.     Copyright: © 1999 by Apple Computer, Inc.
  15.     all rights reserved.
  16.     
  17.     Disclaimer:
  18.     You may incorporate this sample code into your applications without
  19.     restriction, though the sample code has been provided "AS IS" and the
  20.     responsibility for its operation is 100% yours.  However, what you are
  21.     not permitted to do is to redistribute the source as "DSC Sample Code"
  22.     after having made changes. If you're going to re-distribute the source,
  23.     we require that you make it clear in the source that the code was
  24.     descended from Apple Sample Code, but that you've made changes.
  25.     
  26.     Change History (most recent first):
  27.     10/16/99 created by John Montbriand
  28. */
  29.  
  30. #include "History.h"
  31. #include <stddef.h>
  32. #include <Memory.h>
  33. #include <TextUtils.h>
  34. #include <Errors.h>
  35. #include <string.h>
  36.  
  37.  
  38.  
  39. /* individual elements in the history are stored in a linked
  40.     list.  it's a two way list for easy insertions and deletions. */
  41. typedef struct HistoryStruct HistoryRecord;
  42. typedef HistoryRecord *HistoryPtr, **HistoryHandle;
  43. struct HistoryStruct {
  44.     HistoryHandle prev, next;
  45.     char url[1];
  46.     unsigned char printName[1];
  47. };
  48.  
  49. /* the history data handle contains a list of history
  50.     elements along with a pointer to the current
  51.     element being displayed. */
  52. struct HistoryData {
  53.     HistoryHandle first, last;
  54.     HistoryHandle current;
  55. };
  56.  
  57.  
  58.  
  59. /* NewHistory creats a new history and returns
  60.     a handle to it. Here, we just return an handled
  61.     set to zero. */
  62. HistoryDataHandle NewHistory(void) {
  63.     return (HistoryDataHandle) NewHandleClear(sizeof(HistoryData));
  64. }
  65.  
  66.  
  67. /* DisposeHistory disposes of a history and all of the
  68.     structures allocated for it. */
  69. void DisposeHistory(HistoryDataHandle hd) {
  70.     HistoryHandle rover, temp;
  71.     rover = (**hd).first;
  72.         /* delete all of the elements in the history list */
  73.     while (rover != NULL) {
  74.         temp = rover;
  75.         rover = (**rover).next;
  76.         DisposeHandle((Handle) temp);
  77.     }
  78.     DisposeHandle((Handle) hd);
  79. }
  80.  
  81.  
  82. /* AddToHistory adds a new element to the history.  Both
  83.     the URL and the printed representation of its url
  84.     are stored.  NOTE:  if we have called GoBack a few times
  85.     before this call, then those previously viewed items
  86.     are removed from the history. This is so if we choose
  87.     GoBack again, then we will arrive at the same link we
  88.     are looking at now.  */
  89. OSErr AddToHistory(HistoryDataHandle hd, char const* url, StringPtr printName) {
  90.     HistoryHandle hrec, rover;
  91.     long strbytes;
  92.         /* create a new history record */
  93.     strbytes = strlen(url);
  94.     hrec = (HistoryHandle) NewHandle(offsetof(HistoryRecord, url) + strbytes + 1 + printName[0] + 1);
  95.     if (hrec == NULL) return memFullErr;
  96.     BlockMoveData(url, (**hrec).url, strbytes + 1);
  97.     BlockMoveData(printName, (**hrec).url + strbytes + 1, printName[0] + 1);
  98.     (**hrec).prev = (**hrec).next = NULL;
  99.         /* clear out records after current.  We do this
  100.         so the next time we choose the go back command,
  101.         we'll arrive at the same one that was the current
  102.         one before this routine was called.  Essentially, the
  103.         user is conducting a depth first search of the html,
  104.         and here we are branching.  */
  105.     while ((**hd).current != (**hd).last && (**hd).current != NULL) {
  106.         rover = (**(**hd).current).next;
  107.         if ((**rover).next != NULL)
  108.             (**(**rover).next).prev = (**rover).prev;
  109.         else (**hd).last = (**rover).prev;
  110.         if ((**rover).prev != NULL)
  111.             (**(**rover).prev).next = (**rover).next;
  112.         else (**hd).first = (**rover).next;
  113.         DisposeHandle((Handle) rover);
  114.     }
  115.         /* add the new record to the end of the list */
  116.     if ((**hd).first == NULL)
  117.         (**hd).first = (**hd).last = hrec;
  118.     else {
  119.         (**hrec).prev = (**hd).last;
  120.         (**(**hd).last).next = hrec;
  121.         (**hd).last = hrec;
  122.     }
  123.     (**hd).current = hrec;
  124.     return noErr;
  125. }
  126.  
  127.  
  128. /* InHistory returns true if the URL is among the urls
  129.     currently stored in the history. */
  130. Boolean InHistory(HistoryDataHandle hd, char const* url) {
  131.     HistoryHandle rover;
  132.     Boolean isequal;
  133.     for (rover = (**hd).first; rover != NULL; rover = (**rover).next) {
  134.         HLock((Handle) rover);
  135.         isequal = (strcmp(url, (**rover).url) == 0);
  136.         HUnlock((Handle) rover);
  137.         if (isequal) return true;
  138.     }
  139.     return false;
  140. }
  141.  
  142.  
  143. /* CanGoBack returns true if it makes sense to call the
  144.     GoBack command.  i.e. if there are one or more links
  145.     in the history beyond the current one. */
  146. Boolean CanGoBack(HistoryDataHandle hd) {
  147.     if ((**hd).current == NULL)
  148.         return false;
  149.     else return ((**(**hd).current).prev != NULL);
  150. }
  151.  
  152.  
  153. /* GoBack copies the previous url in the history
  154.     into a new handle and returns that handle in
  155.     *url.  It is the caller's responsibility to dispose
  156.     of the handle after it has been used. */
  157. OSErr GoBack(HistoryDataHandle hd, Handle *url) {
  158.     OSErr err;
  159.     HistoryHandle nextcurrent;
  160.     if ((**hd).current == NULL) return paramErr;
  161.     nextcurrent = (**(**hd).current).prev;
  162.     HLock((Handle) nextcurrent);
  163.     err = PtrToHand((**nextcurrent).url, url, strlen((**nextcurrent).url) + 1);
  164.     HUnlock((Handle) nextcurrent);
  165.     if (err == noErr)
  166.         (**hd).current = nextcurrent;
  167.     return err;
  168. }
  169.  
  170.  
  171. /* CanGoForward returns true if it makes sense to call the
  172.     GoForward command.  i.e. if there are one or more links
  173.     in the history ahead of the current one.  This can only
  174.     happen after the user has chosen GoBack one or more
  175.     times. */
  176. Boolean CanGoForward(HistoryDataHandle hd) {
  177.     if ((**hd).current == NULL)
  178.         return false;
  179.     else return ((**(**hd).current).next != NULL);
  180. }
  181.  
  182.  
  183. /* GoForward copies the next url in the history
  184.     into a new handle and returns that handle in
  185.     *url.  It is the caller's responsibility to dispose
  186.     of the handle after it has been used. */
  187. OSErr GoForward(HistoryDataHandle hd, Handle *url) {
  188.     OSErr err;
  189.     HistoryHandle nextcurrent;
  190.     if ((**hd).current == NULL) return paramErr;
  191.     nextcurrent = (**(**hd).current).next;
  192.     HLock((Handle) nextcurrent);
  193.     err = PtrToHand((**nextcurrent).url, url, strlen((**nextcurrent).url) + 1);
  194.     HUnlock((Handle) nextcurrent);
  195.     if (err == noErr)
  196.         (**hd).current = nextcurrent;
  197.     return err;
  198. }
  199.  
  200.  
  201. /* CanGoHome returns true if it makes sense to call the
  202.     GoHome command.  i.e. if there are one or more links
  203.     in the history.  This can only happen after AddToHistory
  204.     has been called one or more times. */
  205. Boolean CanGoHome(HistoryDataHandle hd) {
  206.     return ((**hd).first != (**hd).current) && ((**hd).current != NULL);
  207. }
  208.  
  209.  
  210. /* GoBack copies the first url in the history
  211.     into a new handle and returns that handle in
  212.     *url.  It is the caller's responsibility to dispose
  213.     of the handle after it has been used. */
  214. OSErr GoHome(HistoryDataHandle hd, Handle *url) {
  215.     OSErr err;
  216.     if ((**hd).first == NULL) return paramErr;
  217.     HLock((Handle) (**hd).first);
  218.     err = PtrToHand((**(**hd).first).url, url, strlen((**(**hd).first).url) + 1);
  219.     HUnlock((Handle) (**hd).first);
  220.     if (err == noErr)
  221.         (**hd).current = (**hd).first;
  222.     return err;
  223. }
  224.  
  225.  
  226. /* AppendHistoryToMenu rebuilds the Go menu adding items to the
  227.     bottom of the menu according to the items in the
  228.     history.  The names of the items are the same as
  229.     the printNames provided in the AddToHistory command. */
  230. OSErr AppendHistoryToMenu(HistoryDataHandle hd, MenuHandle theMenu) {
  231.     HistoryHandle rover;
  232.     Str255 title;
  233.     unsigned char *titlep;
  234.     if ((**hd).last == NULL) return noErr;
  235.     AppendMenu(theMenu, "\p(-");
  236.     for (rover = (**hd).last; rover != NULL; rover = (**rover).prev) {
  237.         HUnlock((Handle) rover);
  238.         titlep = (unsigned char *) ((**rover).url + strlen((**rover).url) + 1);
  239.         BlockMoveData(titlep, title, titlep[0] + 1);
  240.         AppendMenu(theMenu, title);
  241.         HUnlock((Handle) rover);
  242.     }
  243.     return noErr;
  244. }
  245.  
  246.  
  247. /* GoToMenuItem copies the itemIndex'th url in the history
  248.     into a new handle and returns that handle in
  249.     *url.  It is the caller's responsibility to dispose
  250.     of the handle after it has been used.  This routine
  251.     should only be called after a menu selection has
  252.     been made in a menu built by AppendHistoryToMenu.  */
  253. OSErr GoToMenuItem(HistoryDataHandle hd, Handle *url, short itemIndex) {
  254.     HistoryHandle rover;
  255.     short i;
  256.     OSErr err;
  257.     if ((**hd).last == NULL) return paramErr;
  258.     for (i = 1, rover = (**hd).last; rover != NULL; rover = (**rover).prev, i++) {
  259.         if (i == itemIndex) {
  260.             HLock((Handle) rover);
  261.             err = PtrToHand((**rover).url, url, strlen((**rover).url) + 1);
  262.             HUnlock((Handle) rover);
  263.             if (err == noErr)
  264.                 (**hd).current = rover;
  265.             return err;
  266.         }
  267.     }
  268.     return noErr;
  269. }